circular object array

94

// CPP program to demonstrate use of circular
// array using extra memory space
#include <bits/stdc++.h>
using namespace std;
void print(char a[], int n, int ind)
{
    // Create an auxiliary array of twice size.
    char b[(2 * n)];
 
    // Copy a[] to b[] two times
    for (int i = 0; i < n; i++)
        b[i] = b[n + i] = a[i];
 
    // print from ind-th index to (n+i)th index.
    for (int i = ind; i < n + ind; i++)
        cout << b[i] << " ";
}
 
// Driver code
int main()
{
    char a[] = { 'A', 'B', 'C', 'D', 'E', 'F' };
    int n = sizeof(a) / sizeof(a[0]);
    print(a, n, 3);
    return 0;
}
// CPP program to demonstrate the use of circular
// array without using extra memory space
#include <bits/stdc++.h>
using namespace std;
 
// function to print circular list starting
// from given index ind.
void print(char a[], int n, int ind)
{
    // print from ind-th index to (n+i)th index.
    for (int i = ind; i < n + ind; i++)
        cout << a[(i % n)] << " ";
}
 
// Driver code
int main()
{
    char a[] = { 'A', 'B', 'C', 'D', 'E', 'F' };
    int n = sizeof(a) / sizeof(a[0]);
    print(a, n, 3);
    return 0;
}
// Java program to demonstrate use of circular
// array using extra memory space
import java.util.*;
import java.lang.*;
 
public class GfG{
     
    // function to print circular list
    // starting from given index ind.
    public static void print(char a[], int n,
                                   int ind){
         
        // print from ind-th index to
        // (n+i)th index.
        for (int i = ind; i < n + ind; i++)
            System.out.print(a[(i % n)] + " ");
    }
     
    // driver code
    public static void main(String argc[]){
        char[] a = new char[]{ 'A', 'B', 'C',
                             'D', 'E', 'F' };
        int n = 6;
        print(a, n, 3);
    }
}
 
/* This code is contributed by Sagar Shukla */
<script>
// javascript program to demonstrate use of circular
// array using extra memory space
 
    // function to print circular list
    // starting from given index ind.
    function print(a, n, ind)
    {
 
        // print from ind-th index to
        // (n+i)th index.
        for (var i = ind; i < n + ind; i++)
            document.write(a[parseInt(i % n)] + " ");
    }
 
    // Driver code
        var a =[ 'A', 'B', 'C', 'D', 'E', 'F' ];
        var n = 6;
        print(a, n, 3);
 
// This code is contributed by Rajput-Ji
</script>
# Python3 program to demonstrate the use of
# circular array without using extra memory space
 
# function to print circular list starting
# from given index ind.
def prints(a, n, ind):
    i = ind
     
    # print from ind-th index to (n+i)th index.
    while i < n + ind :
        print(a[(i % n)], end = " ")
        i = i + 1
 
# Driver Code
a = ['A', 'B', 'C', 'D', 'E', 'F']
n = len(a);
prints(a, n, 3);
 
# This code is contributed by rishabh_jain
<?php
// PHP program to demonstrate use
// of circular array without using
// extra memory space
 
// function to print circular list
// starting from given index ind.
function print($a, $n, $ind)
{
    // print from ind-th index
    // to (n+i)th index.
    for ($i = $ind; $i < $n + $ind; $i++)
        echo $a[($i % $n)] . " ";
}
 
// Driver Code
$a = array( 'A', 'B', 'C',
            'D', 'E', 'F' );
$n = count($a);
print($a, $n, 3);
 
// This code is contributed by Sam007
?>

Comments

Submit
0 Comments